{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6d92f840",
   "metadata": {},
   "source": [
    "#### 回归问题实战"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1f1d4aa8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21e8909b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# y = wx + b\n",
    "# 计算loss\n",
    "def compute_error_for_line_given_points(b, w, points):\n",
    "    totalError = 0\n",
    "    for i in range(0, len(points)):\n",
    "        x = points[i, 0]\n",
    "        y = points[i, 1]\n",
    "        # computer mean-squared-error\n",
    "        totalError += (y - (w * x + b)) ** 2\n",
    "    # average loss for each point\n",
    "    return totalError / float(len(points))\n",
    "\n",
    "# 每次完整总的梯度的计算及更新\n",
    "def step_gradient(b_current, w_current, points, learningRate):\n",
    "    b_gradient = 0\n",
    "    w_gradient = 0\n",
    "    N = float(len(points))\n",
    "    for i in range(0, len(points)):\n",
    "        x = points[i, 0]\n",
    "        y = points[i, 1]\n",
    "        # grad_b = 2(wx + b - y)\n",
    "        b_gradient += (2/N) * ((w_current * x + b_current) - y)\n",
    "        # grad_w = 2(wx + b - y) * x\n",
    "        w_gradient += (2/N) * x * ((w_current * x + b_current) - y)\n",
    "    # update w'\n",
    "    new_b = b_current - (learningRate * b_gradient)\n",
    "    new_w = w_current - (learningRate * w_gradient)\n",
    "    return [new_b, new_w]\n",
    "\n",
    "def gradient_descent_runner(points, starting_b, starting_w, learningRate, num)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
